package
{
	import caurina.transitions.Tweener;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import logout.level.AbstractLevel;
	import logout.level.LevelFactory;
	import logout.pv.sceneloader.SceneLoader;
	
	import org.papervision3d.Papervision3D;
	import logout.net.webServices.WebServiceConnector;
	import flash.events.Event;
	import bi.conf.Configuration;
	
	
	/**
	 * Gere l'ensemble des phases du jeu, instancie les singletons etc
	 * @param mainLayer
	 * @return 
	 * 
	 */	
	public class Application extends EventDispatcher
	{
		
		//__________________________________________________________________________________
		//																			  STATIC
		public static var instance : Application;
		
		public static function start( mainLayer : Sprite ) : void {
			instance = new Application( mainLayer );
		}
		
		
		// GETTER SETTER
		
		public function get previousLevelName() : String {
			return _previousLevelName;
		}
		
		
		//__________________________________________________________________________________
		//																		 CONSTRUCTOR
		
		/**
		 * Gere l'ensemble des phases du jeu, instancie les singletons etc
		 * @param mainLayer : sprite principale ( added au moment de la construction )
		 * @return 
		 * 
		 */		
		public function Application( mainLayer : Sprite )
		{
			super();
			if( instance != null) throw new Error( " Application deja instancié" );
			this.mainLayer = mainLayer;
			
			SceneLoader.start();
			WebServiceConnector.start();
			Tweener.init( mainLayer.stage );
			Papervision3D.VERBOSE = false;
			
			startHome();
		}
		
		/**
		 * user authentifie sur HomeLevel
		 * session ok , c'est parti
		 */		
		public function onLogged( user : User ) : void {
			this.user = user;			
			openLevel( user.currentLevel );
		}
		
		
		//__________________________________________________________________________________
		//																			PRIVATES
		
		public function openLevel( name : String ) : void {
			trace( "Appliction.openLevel");
			if( currentLevel ) closeCurrentLevel();
			currentLevel = LevelFactory.getLevel( name );
			addChild( currentLevel ) ;
			currentLevel.start();
		}
		
		private function closeCurrentLevel() : void {
			Configuration.getInstance( "scene").dispose();
			_previousLevelName = currentLevel.levelName;
			currentLevel.dispose();
		}
		
		/**
		 * Lance la page de Home
		 * 
		 */		
		private function startHome() : void {
			//openLevel( LevelFactory.HOME_LEVEL );
			openLevel( LevelFactory.HALL_LEVEL );
			//openLevel( LevelFactory.KNOW_LEVEL );
			//openLevel( LevelFactory.GOT_LEVEL );
		}
		
		//_____________________________________________
		//							proxy sur mainLayer
		
		private function addChild( child : DisplayObject ) : DisplayObject {	return mainLayer.addChild( child ); }
		private function removeChild( child : DisplayObject ) : DisplayObject {	return mainLayer.removeChild( child ); }
		
		
		/**
		* Clip principal, enfant direct de "root" ( Log_Out.as )<br>
		* tous est contenu dans mainLayer, amen
		*/		
		private var mainLayer : Sprite;
		
		/**
		* Contient et syncronise les infos user
		*/		
		private var user : User;
		
		/**
		* Level actuel
		*/		
		private var currentLevel : AbstractLevel;

		/**
		* nom du Level precedant
		*/		
		private var _previousLevelName : String = "know";
		
	}
}